function index_ = taskCohTimeLearn_interleaved_timeQuest(varargin)
%Do a training time Quest for the interleaved cohTimeLearn task
%
%   index_ = taskCohTimeLearn_interleaved_timeQuest(varargin)
%
%   optional eye tracker for fixation
%   lpHID choices
%
%   index_ specifies the new instance in ROOT_STRUCT.dXtask

% copyright 2008 Benjamin Heasly University of Pennsylvania

% name for this task
name = mfilename;

if nargin >= 2

    % ASL and sound info
    settings = varargin{1};

    % number of trials
    numTrials = varargin{2};

    varargin(1:2) = [];
else
    warning(sprintf('%s using default values', mfilename));
    settings.useASL = true;
    settings.mouseMode = false;
    settings.mute = false;
    settings.dotDir = [0 180];
    settings.viewingTimes = [100 200 400 800];
    numTrials = 10;
end

% number of trials and nuber of dXtc conditons
%   give the number of blocks
nVT = length(settings.viewingTimes);
numBlocks = ceil(numTrials/length(settings.dotDir)/nVT);

% a name for each viewing time
for ii = 1:nVT
    qNames{ii} = sprintf('timeQ81_from%dms\n', settings.viewingTimes(ii));
end

% a guess value for each timeQuest
%   that matches the preceding coherence quests
qGuesses = num2cell(settings.viewingTimes);

% dXquest to asses 81% correct viewing time at each threshold coherence
arg_dXquest = { ...
    'stimRange',    [10, 3000], ...
    'stimValues',   [], ...
    'refStim',      200, ...
    'blankStim',	[], ...
    'guessStim',    qGuesses, ...
    'Tstd',         9, ...
    'estimateType', 'mean', ...
    'psychParams',  [0 2.5 .01 .5], ...
    'goPastConvergence', true, ...
    'name',         qNames,...
    'ptr',          {{'dXtc', 3, 'value'}}, ...
    'showPlot',     false, ...
    'doEndTrial',   false};

% dXtc for dot direction, coherence condition, and for viewing time
tempCohThresholds = linspace(0, 100, nVT);
arg_dXtc = { ...
    'name',     {'dot_dir', 'threshold_coherence', 'viewing_time'}, ...
    'values',	{settings.dotDir, tempCohThresholds, nan}, ...
    'ptr',      {{'dXdots', 1, 'direction'}, {'dXdots', 1, 'coherence'}, {}}};

arg_dXlr = { ...
    'ptr',      {{'dXdots', 1, 'direction'}}};

% args to make statelist polymorphic
ptrs = {'dXtc', 'dXlr'};
vtcon = {'wait', {'dXtc', 3, 'value'}, []};
PQ = @cohTimeLearn_pickQuestCondition;
arg_statelist = {ptrs, vtcon, PQ, settings};

% {'group', reuse, set now, set always}
reswap = {'current', false, true, false};
ta = cohTimeLearn_task_args;
index_ = rAdd('dXtask', 1, {'root', false, true, false}, ...
    'name',         name(5:end), ...
    'blockReps',    numBlocks, ...
    'userData',     tempCohThresholds, ...
    'startTaskFcn', {@cohTimeLearn_interleaved_readCohThresh}, ...
    'helpers', ...
    { ...
    'dXquest',                  nVT,reswap, arg_dXquest; ...
    'dXtc',                     3,  reswap, arg_dXtc; ...
    'dXlr',                     1,  reswap, arg_dXlr; ...
    'gXcohTimeLearn_hardware',	1,  true,   {settings}; ...
    'gXcohTimeLearn_graphics',	1,  true,	{settings}; ...
    'gXcohTimeLearn_statelist',	1,  false,	arg_statelist; ...
    }, ...
    ta{:}, varargin{:});